﻿using Google.Protobuf;
using Proto.Header;
using System;

namespace Bilibili.WebClient
{
    public sealed class GrpcRequestHeaderConfig
    {
        public string AccessKey { get; set; }
        public GrpcRequestHeaderConfig(string accessKey)
        {
            AccessKey = accessKey;
        }
        public const string dalvik_ver = "2.1.0";
        public const string os_ver = "9";
        public const string brand = "Xiaomi";
        public const string model = "MIUI";
        public const string app_ver = "6.7.0";
        public const int build = 6070600;
        public const string channel = "bilibili140";
        public const int network_type = 1;
        public const int network_tf = 0;
        public const string network_oid = "46007";
        public const string cronet = "1.21.0";
        public const string buvid = "XZFD48CFF1E68E637D0DF11A562468A8DC314";
        public const string mobiApp = "android";
        public const string platform = "android";
        public const string env = "prod";
        public const int appId = 1;
        public const string region = "CN";
        public const string language = "zh";
        public string GetFawkesreqBin()
        {
            var msg = new FawkesReq
            {
                Appkey = mobiApp,
                Env = env
            };
            return ToBase64(msg.ToByteArray());
        }
        public string GetMetadataBin()
        {
            var msg = new Metadata
            {
                AccessKey = AccessKey,
                MobiApp = mobiApp,
                Build = build,
                Channel = channel,
                Buvid = buvid,
                Platform = platform
            };
            return ToBase64(msg.ToByteArray());
        }
        public string GetDeviceBin()
        {
            var msg = new Device
            {
                AppId = appId,
                MobiApp = mobiApp,
                Build = build,
                Channel = channel,
                Buvid = buvid,
                Platform = platform,
                Brand = brand,
                Model = model,
                Osver = os_ver
            };
            return ToBase64(msg.ToByteArray());
        }
        public string GetNetworkBin()
        {
            var msg = new Network
            {
                Type = Network.Types.TYPE.Wifi,
                Oid = network_oid
            };
            return ToBase64(msg.ToByteArray());
        }
        public string GetRestrictionBin()
        {
            var msg = new Restriction();
            return ToBase64(msg.ToByteArray());
        }
        public string GetLocaleBin()
        {
            var msg = new Locale
            {
                CLocale = new Locale.Types.LocaleIds(),
                SLocale = new Locale.Types.LocaleIds()
            };
            msg.CLocale.Language = language;
            msg.CLocale.Region = region;
            msg.SLocale.Language = language;
            msg.SLocale.Region = region;
            return ToBase64(msg.ToByteArray());
        }
        public string ToBase64(byte[] data)
        {
            return Convert.ToBase64String(data).TrimEnd('=');
        }
    }
}